package com.xiyu.service.service.infra.devops;

import com.xiyu.service.vo.infra.devops.projectVersion.ProjectVersionGetByProjectOutput;
import com.xiyu.service.vo.infra.devops.projectVersion.ProjectVersionSingleGetOutput;
import com.xiyu.service.convert.infra.devops.ProjectVersionConvert;
import com.xiyu.service.framework.web.web.core.pojo.PageResult;
import com.xiyu.service.util.entity.EntityUtils;
import com.xiyu.service.vo.infra.devops.projectVersion.ProjectVersionUpdateInput;
import com.xiyu.service.vo.infra.devops.projectVersion.ProjectVersionCreateInput;
import com.xiyu.service.model.devops.project.DevopsProjectVersion;
import com.xiyu.service.repository.devops.project.DevopsProjectVersionRepository;
import com.xiyu.service.vo.infra.devops.projectVersion.ProjectVersionPageQueryOutput;
import com.xiyu.service.vo.infra.devops.projectVersion.ProjectVersionPageQueryInput;
import jakarta.annotation.Resource;
import org.babyfish.jimmer.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Optional;

import static com.xiyu.service.errorCode.infra.devops.ProjectVersionErrorCode.DEVOPS_PROJECT_VERSION_NOT_EXIST;
import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;

/**
 * 项目版本管理 Service 实现类
 */
@Service
@Validated
public class ProjectVersionServiceImpl implements ProjectVersionService {

    @Resource
    private DevopsProjectVersionRepository devopsProjectVersionRepository;




    @Override
    public PageResult<ProjectVersionPageQueryOutput> pageQuery(ProjectVersionPageQueryInput inputVO) {
        Page<DevopsProjectVersion> pageDevopsProjectVersion = devopsProjectVersionRepository.pageQuery(inputVO);
        List<ProjectVersionPageQueryOutput> listDevopsProjectVersion = ProjectVersionConvert.INSTANCE.pageQueryOutputConvert(pageDevopsProjectVersion.getRows());
        return new PageResult<>(listDevopsProjectVersion, pageDevopsProjectVersion.getTotalRowCount());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(ProjectVersionCreateInput inputVO) {
        DevopsProjectVersion newDevopsProjectVersion = ProjectVersionConvert.INSTANCE.createInputConvert(inputVO);
        newDevopsProjectVersion = devopsProjectVersionRepository.insert(newDevopsProjectVersion);
        return newDevopsProjectVersion.id();

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(ProjectVersionUpdateInput inputVO) {
        Optional<DevopsProjectVersion> optionalOldDevopsProjectVersion = devopsProjectVersionRepository.findById(inputVO.getId());
        if(optionalOldDevopsProjectVersion.isEmpty()){
            throw exception(DEVOPS_PROJECT_VERSION_NOT_EXIST);
        }
        DevopsProjectVersion updateDevopsProjectVersion = ProjectVersionConvert.INSTANCE.updateInputConvert(inputVO);
        if (!EntityUtils.isEquals(optionalOldDevopsProjectVersion.get(), updateDevopsProjectVersion))
            devopsProjectVersionRepository.update(updateDevopsProjectVersion);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleted(Long id) {
        Optional<DevopsProjectVersion> optionalOldDevopsProjectVersion = devopsProjectVersionRepository.findById(id);
        if(optionalOldDevopsProjectVersion.isEmpty()){
            throw exception(DEVOPS_PROJECT_VERSION_NOT_EXIST);
        }
        devopsProjectVersionRepository.deleteById(id);
        return null;
    }

    @Override
    public ProjectVersionSingleGetOutput singleGet(Long id) {
        Optional<DevopsProjectVersion> optionDevopsProjectVersion = devopsProjectVersionRepository.findById(id);
        if(optionDevopsProjectVersion.isEmpty()){
            throw exception(DEVOPS_PROJECT_VERSION_NOT_EXIST);
        }
        return ProjectVersionConvert.INSTANCE.singleGetOutputConvert(optionDevopsProjectVersion.get());

    }

    @Override
    public List<ProjectVersionGetByProjectOutput> getByProject(Long projectId) {
        return ProjectVersionConvert.INSTANCE.getByProjectOutputConvert(devopsProjectVersionRepository.getByProjectId(projectId));
    }

}
